package cn.javabb.project.service;

import cn.javabb.common.exception.BizException;
import cn.javabb.common.exception.GenException;
import cn.javabb.framework.config.DataSourceConfig;
import cn.javabb.project.entity.SysDataSource;
import cn.javabb.project.mapper.SysDataSourceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @desc:
 * @author: javabb (javabob(a)163.com)
 * @create: 2021/04/25 16:20
 */
@Slf4j
@Service
public class SysDataSourceService extends ServiceImpl<SysDataSourceMapper, SysDataSource> {

    public List<SysDataSource> list(SysDataSource sdc) {
        return baseMapper.list(sdc);
    }

    public DataSourceConfig getDscById(Long id) {
        DataSourceConfig dataSourceConfig = Optional.ofNullable(this.getById(id)).map(ds -> {
            DataSourceConfig dsc = DataSourceConfig.builder()
                    .url(ds.getUrl())
                    .driverName(ds.getDriverName())
                    .username(ds.getUsername())
                    .password(ds.getPassword()).build();
            return dsc;
        }).orElseThrow(()->new GenException("获取数据源错误"));
        return dataSourceConfig;
    }
}
